﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using MedicineTracker.Model;
using System.Collections.ObjectModel;

namespace MedicineTracker.ViewModel
{
    public class AdministrationsViewModel : Database, INotifyPropertyChanged
    {
        private ObservableCollection<Administration> _administrations;

        public ObservableCollection<Administration> Administrations
        {
            get { return _administrations; }
        }

        public AdministrationsViewModel()
        {
            _administrations = new ObservableCollection<Administration>();
            using (Connection)
            {
                try
                {
                    Connection.Open();
                    Command.CommandText = "SELECT ad.id, ad.administration_date AS date, ad.animal_no, ad.quantity_administered AS qty,ad.administered_by, ad.site AS site_id, ad.seven_day_review AS review, ad.review_notes AS notes, ad.treatment_finished AS finished,ad.reason_for_administration AS reason_id, ad.milk_ok, ad.meat_ok,ad.purchase AS purchase_id, med.name AS med, pur.batch_no AS batch,site.description AS site,re.reasonFROM administrations adINNER JOIN purchases pur ON pur.id = ad.purchaseINNER JOIN medicines med ON med.id = pur.medicine_idINNER JOIN treatment_sites site ON site.id = ad.siteINNER JOIN reasons re ON re.id = ad.reason_for_administration";
                    Command.Connection = Connection;
                    DataReader = Command.ExecuteReader();
                    while (DataReader.Read())
                    {
                        _administrations.Add(new Administration
                        {
                            ID = Convert.ToInt32(DataReader["id"]),
                            AdministeredBy = (string)DataReader["add_by"],
                            AdministrationDate = (DateTime)DataReader["date"],
                            Animal = (string)DataReader["animal_no"],
                            Batch = (string)DataReader["batch"],
                            MeatOk = (DateTime)DataReader["meat_ok"],
                            Medicine = (string)DataReader["med"],
                            MilkOk= (DateTime)DataReader["milk_ok"],
                            Notes = (string)DataReader["notes"],
                            PurchaseID = Convert.ToInt32(DataReader["purchase_id"]),
                            Quantity = Convert.ToInt32(DataReader["qty"]),
                            Reason = (string)DataReader["reason"],
                            ReasonID = Convert.ToInt32(DataReader["reason_id"]),
                            Review = (string)DataReader["review"],
                            Site = (string)DataReader[""],
                            SiteID = Convert.ToInt32(DataReader["site_id"]),
                            TreatmentFinised = (DateTime)DataReader["finished"]
                        });
                    }
                }
                catch
                {
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
